package com.polaris.live.dialog

import android.content.Context
import android.view.View
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogUniversalButtonBinding

/**
 * 通用按钮弹窗
 *
 * @author Created by lucas on 2023/10/26 14:03
 */
class UniversalButtonDialog(
    context: Context,
    private val title: String?,
    private val content: String,
    private val confirmText: String?,
    private val confirmListener: OnClickListener?,
    private val cancelText: String?,
    private val cancelListener: OnClickListener?
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogUniversalButtonBinding

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_universal_button
    }

    override fun onCreate() {
        super.onCreate()

        mBinding = DialogUniversalButtonBinding.bind(popupImplView)

        if (title.isNullOrEmpty()) {
            mBinding.tvTitle.visibility = View.GONE
        } else {
            mBinding.tvTitle.visibility = View.VISIBLE
            mBinding.tvTitle.text = title
        }

        mBinding.tvContent.text = content

        if (!confirmText.isNullOrEmpty()) {
            mBinding.tvConfirm.text = confirmText
        }
        if (!cancelText.isNullOrEmpty()) {
            mBinding.tvCancel.text = cancelText
        }

        mBinding.tvConfirm.setOnSingleClickListener {
            confirmListener?.onClick(it)
            dismiss()
        }

        mBinding.tvCancel.setOnSingleClickListener {
            cancelListener?.onClick(it)
            dismiss()
        }
    }

    class Builder(
        private val context: Context,
        private val content: String
    ) {

        private var title: String? = null
        private var confirmText: String? = null
        private var cancelText: String? = null
        private var confirmListener: OnClickListener? = null
        private var cancelListener: OnClickListener? = null

        fun setTitle(title: String): Builder {
            this.title = title

            return this
        }

        fun setConfirmText(text: String): Builder {
            this.confirmText = text

            return this
        }

        fun setCancelText(text: String): Builder {
            this.cancelText = text

            return this
        }

        fun setConfirmListener(l: OnClickListener): Builder {
            this.confirmListener = l

            return this
        }

        fun setCancelListener(l: OnClickListener): Builder {
            this.cancelListener = l

            return this
        }

        fun build(): UniversalButtonDialog {
            return UniversalButtonDialog(
                context,
                title,
                content,
                confirmText,
                confirmListener,
                cancelText,
                cancelListener
            )
        }
    }
}